const Model = require('./index')

class FriendLinkModel extends Model {
  async find () {
    const sql = 'select fl.* from friend_link fl'
    try {
      const data = await this.db.query(sql)
      return data
    } catch (error) {
      throw new Error(error)
    }
  }

  async create (attrs) {
    const createTime = this.dateFormat(new Date())
    const { name, link, logo } = attrs
    const sql = 'insert into friend_link (name, link, logo, createTime) values(?, ?, ?, ?)'
    try {
      await this.db.query(sql, [name, link, logo, createTime])
    } catch (error) {
      throw new Error(error)
    }
  }

  async update (id, attrs) {
    const updateTime = this.dateFormat(new Date())
    const { name, link, icon } = attrs
    const { columnSet, values } = this.multipleColumnSet({
      name,
      link,
      icon,
      updateTime
    })
    const sql = `update friend_link set ${columnSet} where id = ?`
    try {
      await this.db.query(sql, [...values, id])
    } catch (error) {
      throw new Error(error)
    }
  }

  async delete (ids) {
    const sql = 'delete from friend_link where id in (?)'
    try {
      await this.db.query(sql, [ids])
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new FriendLinkModel('friend_link')
